﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;

namespace ModuleDuoc
{
    public partial class frmViTri : ModuleDuoc.Base.frmAddBase
    {
        string sql;
        BE.Zd_ViTri beViTri = new BE.Zd_ViTri();
        BC.Zd_ViTriBC bcViTri = new BC.Zd_ViTriBC();
        DataTable dt = new DataTable();
        string type;
        public frmViTri()
        {
            InitializeComponent();
            dgvData.AutoGenerateColumns = false; 
        }

        private void frmViTri_Load(object sender, EventArgs e)
        {
            getViTri();
        }
        private void getViTri()
        {
            dt = bcViTri.GetAll();
            dgvData.DataSource = dt;
        }

        private void dgvData_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                txtMaViTri.Text = dgvData.CurrentRow.Cells["MaViTri"].Value.ToString();
                txtTenViTri.Text = dgvData.CurrentRow.Cells["TenViTri"].Value.ToString();
                txtGhiChu.Text = dgvData.CurrentRow.Cells["GhiChu"].Value.ToString();
                type = "update";
            }
            catch (Exception ex)
            { 
                MessageBox.Show(ex.Message); 
            }
        }
        public override void btnThemMoi_Click(object sender, EventArgs e)
        {
            base.btnThemMoi_Click(sender, e);
            type = "insert";
            clear(); // goi phuong thuc xoa de nhap moi truong du lieu
        }
        private void clear()// xoa trang cac truong du lieu khi them moi
        {
            txtMaViTri.Focus(); ;
            txtMaViTri.Text = "";
            txtTenViTri.Text = "";
            txtGhiChu.Text = "";
        }
        public override void btnSave_Click(object sender, EventArgs e)
        {
            base.btnSave_Click(sender, e);
            try
            {
                beViTri.MaViTri = txtMaViTri.Text;
                beViTri.TenViTri = txtTenViTri.Text;
                beViTri.GhiChu = txtGhiChu.Text;

                if (type == "insert")
                {
                    bcViTri.Insert(beViTri);
                    MessageBox.Show("Thêm mới thành công", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (type == "update")
                {
                    beViTri.ViTri_ID = Int32.Parse(dgvData.CurrentRow.Cells["ViTri_ID"].Value.ToString());
                    bcViTri.Update(beViTri);
                    MessageBox.Show("Sửa thành công", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                clear();
                getViTri();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public override void btnXoa_Click(object sender, EventArgs e)
        {
            base.btnXoa_Click(sender, e);
            if (type == "update")
            {
                if (MessageBox.Show("Xác nhận xóa vị trí?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    bcViTri.Delete(BE.Zd_ViTri.Zd_ViTriFields.ViTri_ID, Int32.Parse(dgvData.CurrentRow.Cells["ViTri_ID"].Value.ToString()));
                }
                getViTri();
            }
        }        
    }
}
